package jdbc;

import java.sql.*;

public class Connectivity {

	public static void main(String[] args) {

		Connection baglanti = null;
		PreparedStatement statement = null;
		// Statement statement = null;
		String query = "select * from course where credit = ? and semester = ?";
		// String query = "select * from course";

		try {

			Class.forName("oracle.jdbc.driver.OracleDriver");
			baglanti = DriverManager.getConnection(
					"jdbc:oracle:thin:@//127.0.0.1:1521/xe", "system", "hasan");

			// statement = baglanti.createStatement();
			statement = baglanti.prepareStatement(query);

			statement.setInt(1, 3);
			statement.setString(2, "spring");

			ResultSet result = statement.executeQuery();
			ResultSetMetaData meta = result.getMetaData();

			/*statement = baglanti
					.prepareStatement("update course set semester = ? where code = ? ");
			statement.setString(1, "fall");
			statement.setString(2, "BLM007");
			statement.executeUpdate();*/
			while (result.next())
				for (int i = 1; i <= meta.getColumnCount(); i++)
					System.out.printf("%-8s\t", result.getObject(i));
			/*
			 * query = "insert into Course values(?, ?, ?, ?)"; statement =
			 * baglanti.prepareStatement(query); statement.setString(1,
			 * "BLM007"); statement.setString(2, "Embeded");
			 * statement.setString(3, "Spring"); statement.setInt(4, 3);
			 * 
			 * statement.executeUpdate();
			 */
			/*
			 * for (int i = 1; i <= meta.getColumnCount(); i++)
			 * System.out.printf("%-8s\t", meta.getColumnName(i));
			 * System.out.println();
			 * 
			 * while (result.next()) { for (int i = 1; i <=
			 * meta.getColumnCount(); i++) System.out.printf("%-8s\t",
			 * result.getObject(i)); System.out.println();
			 * 
			 * }
			 */

		} catch (SQLException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		} finally {
			try {
				baglanti.close();
				statement.close();
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(1);
			}
		}

	}

}
